package com.hzqy.web.ac;

import com.hzqy.service.commons.CommonsServiceBean;
import com.hzqy.web.login.SessionKey;
import com.hzqy.web.vo.PmsOrgVo;
import com.hzqy.web.vo.SysProductVo;
import com.hzqy.web.vo.UserHistoryTimeVo;
import com.hzqy.web.vo.UserHistoryVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/commons")
public class CommonsAction {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	CommonsServiceBean commonsServiceBean;
	/**
	 * 根据产品包id拿到产品包详细信息
	 * @param f_sp_id
	 * @return
	 */
	@RequestMapping("/select_product")
	@ResponseBody
	public Object selectProduct(int f_sp_id){
		LOG.debug("selectProduct--f_sp_idf_sp_id:"+f_sp_id);
		SysProductVo sysProductVo=commonsServiceBean.selectProduct(f_sp_id);
		return sysProductVo;
	}
	
	/**
	 * 根据分类ID得到分类对应产品列表，一个分类可对应多个产品（产品有产品ID和服务ID(10)区分，通过产品类型type区分）
	 * @param f_catalog_id
	 * @return
	 */
	@RequestMapping("/catalog_product")
	@ResponseBody
	public Object selectProductListByCatalogId(int f_catalog_id) {
		List<SysProductVo> productList = commonsServiceBean.selectProductListByCatalogId(f_catalog_id);
		return productList;
	}
	/**
	 * 根据产品包id查询产品包列表
	 * @param f_sp_id
	 * @return
	 */
	@RequestMapping("/select_products")
	@ResponseBody
	public Object selectProductListByProductId(int f_sp_id){
		List<SysProductVo> productList = commonsServiceBean.selectProductListByProductId(f_sp_id);
		return productList;
	}
	/**
	 * 根据内容id反查分类获得产品列表
	 * @param f_cc_id
	 * @return
	 */
	@RequestMapping("/content_products")
	@ResponseBody
	public Object selectProductListByContentId(int f_cc_id){
		List<SysProductVo> productList = commonsServiceBean.selectProductListByContentId(f_cc_id);
		return productList;
	}
	/**
	 * 根据产品ID获得对应绑定企业列表
	 * @param f_sp_id
	 * @return
	 */
	@RequestMapping("/selectOrgCodeByProductId")
	@ResponseBody
	public Object selectOrgCodeByProductId(int f_sp_id) {
		List<PmsOrgVo> orgList = commonsServiceBean.selectOrgCodeByProductId(f_sp_id);
		return orgList;
	}
	
	@RequestMapping("/selectHistory")
	@ResponseBody
	public Object selectHistory(UserHistoryVo uh) {
		UserHistoryVo uhv= commonsServiceBean.selectHistory(uh);
		if(uhv == null)
			uhv = new UserHistoryVo();
		return uhv;
	}
	
	/**
	 * 获取单个用户的播放历史清单，按时间倒序排列，以下param参数为必传参数
	 * @param uhv  f_sp_id 产品包id，可传多个产品，用英文半角逗号分隔
	 * @param uhv  f_cp_cprid 图片类型
	 * @param uhv  f_uvh_type 内容为0，包为1----这个参数可传可不传了，但考虑到以前的老数据未正确区分，所以暂时保留
	 * @param pageNo 从第几条开始获取
	 * @param pageSize 取多少条历史记录
	 * @return
	 */
	@RequestMapping("/selectHistoryList")
	@ResponseBody
	public Object selectHistoryList(HttpSession session,UserHistoryVo uhv,int pageNo, int pageSize) {
		List<UserHistoryVo> uhvList = null;
		Object serverIdObj = session.getAttribute(SessionKey.SESSION_SERVERID);
		if(serverIdObj != null) { //当session超时过期后返回null
			uhv.setF_uvh_serverid((String)serverIdObj);
		}
		if(uhv.getF_uvh_type() == 0 || uhv.getF_uvh_type() == 2) { //内容,2:阅读wake瑜伽播放记录
			uhvList= commonsServiceBean.selectHistoryListByContent(uhv,pageNo,pageSize);
		} else { //包
			uhvList= commonsServiceBean.selectHistoryListByMultirate(uhv,pageNo,pageSize);
		}
		return uhvList;
	}
	
	@RequestMapping("/updateHistory")
	@ResponseBody
	public Object updateHistory(UserHistoryVo uh) {
		UserHistoryVo uhv= commonsServiceBean.selectHistory(uh);
		int flag;
		if(uhv!=null){
			flag = commonsServiceBean.updateHistory(uh);
		}else{
			flag = commonsServiceBean.insertHistory(uh);
		}
		return flag;
	}
	@RequestMapping("/updateHistoryTime")
	@ResponseBody
	public Object updateHistoryTime(UserHistoryTimeVo uht,String focusIdwai) {
		int flag = commonsServiceBean.updateHistoryTime(uht,focusIdwai);
		return flag;
	}
	@RequestMapping("/selectHistoryTime")
	@ResponseBody
	public Object selectHistoryTime(String f_uvht_serverid,String beginDate,String endDate,Long seconds) {
		List<UserHistoryTimeVo> count = commonsServiceBean.selectHistoryTime(f_uvht_serverid,0,0,null,beginDate,endDate,seconds);
		return count;
	}
	@RequestMapping("/selectOnDemandCount")
	@ResponseBody
	public Object selectOnDemandCount(UserHistoryVo uh) {
		Map<String,Object> map = new HashMap<String,Object>();
		int count = commonsServiceBean.selectOnDemandCount(uh);
		map.put("count", count);
		return map;
	}
	
	@RequestMapping("/picPlayTime")
	@ResponseBody
	public Object picPlayTime() {
		Map<String,Object> map = new HashMap<String,Object>();
		return map;
	}

	@RequestMapping("/selectProductList")
	@ResponseBody
	public Object selectProductList(SysProductVo sysProductVo) {
		List<SysProductVo> list = commonsServiceBean.selectProductList(sysProductVo);
		return list;
	}
	//广告详情 用于入日志 wma:业务账号  adsp: 广告投放位置
	@RequestMapping("/adDetails")
	@ResponseBody
	public Object adDetails(HttpServletRequest req,String wma,String adsp,int f_ca_id,int f_cas_id) {
		HttpSession session = req.getSession();
		if("1".equals(adsp)){//开机只显示一次
			session.setAttribute(adsp+"_"+wma,f_ca_id);
		}
		commonsServiceBean.analysisOfAdvertisingTimes(req,wma,adsp,f_ca_id,f_cas_id);
		Map<String,Object> map = new HashMap<String,Object>();
		String current_url = req.getRequestURL().toString();
		String current_par = req.getQueryString();
		if(current_par != null){
			current_url = current_url+"?"+current_par;
		}
		LOG.info(current_url);
		return map;
	}
}
